package cn.com.qiancheng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.com.qiancheng.dto.Results;
import cn.com.qiancheng.util.DbUtil;

public class ResultsDao {
	public int insertRes(Results res) {
		int t = 0;
		try {
			Connection con = DbUtil.openConnection();
			String sql = "SELECT f_id FROM t_unfinish WHERE f_stu_id = ? AND f_eva_id =?";
			PreparedStatement stam = con.prepareStatement(sql);
			stam.setInt(1, res.getStuId());
			stam.setInt(2, res.getEvaId());
			ResultSet rs = stam.executeQuery();
			if (rs.next()) {
				int u_id = rs.getInt("f_id");
				for (int i = 0; i < res.getqId().size(); i++) {
					sql = "INSERT INTO t_results(u_id,qu_id,an_id) VALUES(?,?,?)";
					stam = con.prepareStatement(sql);
					stam.setInt(1, u_id);
					stam.setInt(2, res.getqId().get(i));
					stam.setInt(3, res.getAnId().get(i));
					t = stam.executeUpdate();
				}
				if (t != 0) {
					sql = "UPDATE t_unfinish SET e_flag = 1 WHERE f_id = ?";
					stam = con.prepareStatement(sql);
					stam.setInt(1, u_id);
					t = stam.executeUpdate();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return t;
	}
}
